package com.janko.springbootbase.work;

import com.janko.springbootbase.work.entity.CarEngin;
import com.janko.springbootbase.work.entity.JsonEntity;
import com.janko.springbootbase.work.entity.Paramert;
import com.janko.springbootbase.work.entity.TechInfoBean;

import java.io.*;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author nyk
 * @Date 2022/3/4 11:41
 * @Version 1.0
 * @Desc 宝马摩托车用到的工具类
 */
public class BMWController {
    public final static Pattern PATTERN_PlateNo =  Pattern.compile("^[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][ABCDEFGHJKLMNPQRSTUVWXY][\\dABCDEFGHJKLNMxPQRSTUVWXYZ]{5,6}$");

    public static void main(String[] args) {
       /* // 读取txtjson文本内容
        String result = readTxtFile("d://GetAllTechInfos.json").toString();
        String outPath = "d://outSql.sql";
        // 添加颜色
//        getColorSql(result);

        // 获取GetAllTechInfos.json数据
//        getAllTechInfosJson(result, outPath);
        LocalDateTime startDate=null;
        LocalDateTime fromStartDate=null;
          DateTimeFormatter.ofPattern("yyyy-MM-dd").format(fromStartDate);*/

        System.out.println(isMactchRegex(PATTERN_PlateNo, "京AD8888A"));
        System.out.println(isMactchRegex(PATTERN_PlateNo, "京A12345"));
        System.out.println(isMactchRegex(PATTERN_PlateNo, "京A123456"));
    }

    public static boolean isMactchRegex(Pattern pattern, CharSequence value) {
        if (value == null || pattern == null) {
            // 提供null的字符串为不匹配
            return false;
        }
        return pattern.matcher(value).matches();
    }

    /**
     * 添加车型、车款和车款参数SQL
     *
     * @param result
     */
    private static void getAllTechInfosJson(String result, String outPath) {
        List<TechInfoBean> list = com.alibaba.fastjson.JSON.parseArray(result, TechInfoBean.class);
        int carTypeOrder = 59;

        for (TechInfoBean bean : list) {
            // car_Type 类型的数据
            StringBuilder sbCarType = new StringBuilder(" INSERT INTO car_type(series_id,name,ename,[order],code,createdon,modifiedon,isEnabled,icon,reverse_icon,status)   VALUES  \n");
            sbCarType.append("(12,N'" + bean.getName() + "',N''," + carTypeOrder + ",N'" + bean.getCode() + "',GETDATE(),GETDATE(),1,N'', N'',0);");
            carTypeOrder++;
            System.out.println("-- 添加车型 \n" + sbCarType.toString());
            outToFile(outPath, sbCarType);


            // 车型的排序
            int carEngineOrder = 1;
            for (CarEngin carEngin : bean.getCarEnginlist()) {
                // 表 car_engine 的新增SQL
                StringBuilder sbCarEngine = new StringBuilder(" INSERT INTO car_engine(car_type_id,name,ename,[order],code,createdOn,modifiedOn,isEnabled,description,status)   VALUES  \n");
                sbCarEngine.append(" (typeId啊,N'" + carEngin.getCarEngineName() + "', N'" + carEngin.getCarEngineEName() + "'," + carEngineOrder + " , N'',GETDATE(),GETDATE(),1,N'', 0), \n");
                carEngineOrder++;
                sbCarEngine = sbCarEngine.delete(sbCarEngine.length() - 2, sbCarEngine.length());
                System.out.println("-- 添加车款 \n" + sbCarEngine.toString());
                outToFile(outPath, sbCarEngine);



                // 表 engine_parmater表新增SQL
                StringBuilder sbEngineParmater = new StringBuilder(" INSERT INTO engine_parmater(engine_id,engine_code,pcode,  pvalue,pevalue,createdon, modifiedon,status)   VALUES  \n");
                for (Paramert paramert : carEngin.getParamertsList()) {
                    sbEngineParmater.append(" (engineId啊,N'', N'" + paramert.getPCode() + "', N'" + paramert.getPValue() + "', N'" + paramert.getPEValue() + "',GETDATE(), GETDATE(),0),\n ");
                }
                // 删除小尾巴
                sbEngineParmater = sbEngineParmater.delete(sbEngineParmater.length() - 2, sbEngineParmater.length());
                System.out.println("-- 添加车款参数 \n" + sbEngineParmater.toString());
                outToFile(outPath, sbEngineParmater);
            }
            System.out.println(" ================================================== ");

        }
    }

    // 添加颜色的脚本内容
    private static void getColorSql(String result) {
        List<JsonEntity> list = com.alibaba.fastjson.JSON.parseArray(result, JsonEntity.class);
        StringBuilder sb = new StringBuilder(" INSERT INTO color_copy   VALUES  ");
        int id = 123;
        for (JsonEntity entity : list) {
            sb.append("('" + id + "','" + entity.getName() + "','" + entity.getCode() + "','" + entity.getIcon() + "',GETDATE(),GETDATE(),null,null,null,0),");
            id++;
        }
        System.out.println(sb.toString());
    }

    /**
     * 写内容到文件中
     *
     * @param filePath 文件路径
     * @param word     内容
     */
    public static void outToFile(String filePath, StringBuilder word) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true)));
            out.write(word.toString() + "\r\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 读取本地Json内容
     *
     * @param filePath
     * @return
     */
    public static StringBuilder readTxtFile(String filePath) {
        StringBuilder sb = new StringBuilder();
        try {
            String encoding = "UTF-8";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
//                    System.out.println(lineTxt);
                    sb.append(lineTxt);
                }
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return sb;
    }
}

